package com.backend.demo.action;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.stereotype.Controller;

import com.backend.demo.entities.Role;
import com.backend.demo.service.IRoleService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 角色管理
 * @author Administrator
 *
 */
@ParentPackage("user")
@Namespace("/jspView")
@InterceptorRefs(value = { @InterceptorRef(value = "loginStack") })
@Controller
public class RoleAction extends ActionSupport implements RequestAware, ModelDriven<Role> {

	private static final long serialVersionUID = 1L;
	
	private Map<String,Object> request;
	
	private Role role;
	
	private InputStream inputStream;
	
	@Resource(name="roleService")
	private IRoleService roleService;
	
	public void setRole(Role role) {
		this.role = role;
	}
	
	public Role getRole() {
		return role;
	}
	
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}
	
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public Role getModel() {
		if(role == null){
			role = new Role();
		}
		return role;
	}
	
	@Action(value="getAllRoles",results={@Result(name=SUCCESS,location="/jspView/roles/roleList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getAllRoles(){
		List<Role> roles = roleService.getAllRoles();
		if(roles != null && roles.size() > 0 ){
			request.put("roles", roles);
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="getRolesByCondition",results={@Result(name=SUCCESS,location="/jspView/roles/roleList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getRolesByCondition(){
		List<Role> roles = roleService.getRolesByCondition(role);
		request.put("roles", roles);
		return SUCCESS;
	}
	
	@Action(value="getRoleById",results={@Result(name=SUCCESS,location="/jspView/roles/editRole.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getRoleById(){
		Role roleNew = roleService.getRoleById(role.getRoleID());
		if(roleNew != null){
			request.put("role", roleNew);
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="addRole",results={@Result(name=SUCCESS,location="/jspView/getAllRoles"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String addRole(){
		int num = roleService.addRole(role);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="editRole",results={@Result(name=SUCCESS,location="/jspView/getAllRoles"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String editRole(){
		int num = roleService.updateRole(role);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="deleteRole",results={@Result(name=SUCCESS,location="/jspView/getAllRoles"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String deleteRole(){
		int num = roleService.delete(role.getRoleID());
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="redirectRole",results={@Result(name=SUCCESS,location="/jspView/roles/addRole.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String redirectRole(){
		return SUCCESS;
	}
	
}
